<?php
/**
 * [DB模型]
 * 
 * @version $Id: Order.php 277 2012-04-24 08:37:00Z linsir123 $
 */
class dbOrder extends ActiveRelation 
{
	/**
	 * @var 数据库表名
	 */
	public $table = 'order';
	
	/**
	 * 取得列表
	 */
	public function getList($start = 0, $offset = 10, $params = array())
	{
		$this->limit($start, $offset);
		$this->_searchSql($params);
		
		///
		return $this->order('id desc')->getResults();
	}
	
	/**
	 * 取得总数
	 */
	public function getListCount($params = array())
	{
		$this->_searchSql($params);
		
		///
		return $this->getCount();
	}
	
	/**
	 * 构造查询SQL
	 */
	private function _searchSql($params)
	{
		if ( ! empty($params['uid'])) 
			$this->where('uid', $params['uid']);
			
		if ( ! empty($params['order_id'])) 
			$this->where('order_id', $params['order_id']);
			
		if ( ! empty($params['item_title'])) 
			$this->where('item_title', array('like', '"%?%"', $params['item_title']));
			
		if ( ! empty($params['start_date'])) 
			$this->where('create_time', array('>=', $params['start_date']));
			
		if ( ! empty($params['end_date'])) 
			$this->where('create_time', array('<=', $params['end_date']));
	}
	
	/**
	 * 订单数
	 */
	public function getOrdersCountByUser($uId)
	{
		return $this->where('uid', $uId)->getCount();
	}
	
	/**
	 * 总返现金额
	 */
	public function getOrdersMoneyByUser($uId)
	{
		return $this->where('uid', $uId)->getSum('commission');
	}
}
